package com.sptpc.pm2;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class Sign extends Activity implements View.OnClickListener {
    private TextView tvRregister,btuDl;
    private EditText etPhone,etCode;
    private SharedPreferences.Editor editors;
    private SharedPreferences prefs;
    CheckBox ckOne;
    String user;
    String pass;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_in);
        etPhone=(EditText)findViewById(R.id.et_phone);
        etCode=(EditText)findViewById(R.id.et_code);
        ckOne=(CheckBox)findViewById(R.id.ck_one);
        tvRregister=(TextView)findViewById(R.id.tv_register);
        tvRregister.setOnClickListener(this);
        btuDl=(TextView)findViewById(R.id.btu_dl);
        btuDl.setOnClickListener(this);
        prefs= PreferenceManager.getDefaultSharedPreferences(this);
        boolean isRemember=prefs.getBoolean("boolead",false);
        if (isRemember){
            Log.d("wang","is") ;
            String users=prefs.getString("userh","");
            String passs=prefs.getString("passh","");
            ckOne.setChecked(true);
            etPhone.setText(users);
            etCode.setText(passs);
            ckOne.setChecked(true);
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.tv_register:
                Intent intent= new Intent(Sign.this,Register.class);
                startActivity(intent);
                break;
            case R.id.btu_dl:
                String inputEtPhone=etPhone.getText().toString();
                String inputEtCode=etCode.getText().toString();
                SharedPreferences pref=getSharedPreferences("data"+inputEtPhone,MODE_PRIVATE);
                user=pref.getString("user","");
                pass=pref.getString("pass","");
                Log.d("wang",pass) ;
                Log.d("wang",user) ;

                if (!(inputEtPhone.equals(user))){
                    Toast.makeText(this,"账号错误",Toast.LENGTH_LONG).show();
                }

                else if (!(inputEtCode.equals(pass))){
                    Toast.makeText(this,"密码错误",Toast.LENGTH_LONG).show();
                }
                else if (inputEtCode.equals(pass) && inputEtPhone.equals(user)){
                    Toast.makeText(this,"登录成功",Toast.LENGTH_LONG).show();
                    Intent intent1=new Intent(Sign.this,MainActivity.class);
                    startActivity(intent1);
                    editors=prefs.edit();
                    if (ckOne.isChecked()){
                        editors.putString("userh",user);
                        editors.putString("passh",pass);
                        editors.putBoolean("boolead",true);
                        Log.d("wang","if") ;
                    }
                    else {
                        editors.clear();
                    }
                    editors.apply();

                }
                else {
                    Toast.makeText(this,"发生未知错误",Toast.LENGTH_LONG).show();
                }
        }
    }
}